home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10244 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.unt.edu!news
  2. From: Steve Fogoros <sfogoros@hsc.unt.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ?? How to dump text files to screen ??
  5. Date: Fri, 15 Mar 1996 23:42:55 -0800
  6. Organization: University of North Texas Health Science Center
  7. Message-ID: <314A70FF.4EC8@hsc.unt.edu>
  8. References: <31430CE8.469E@aol2.com> <4iddva$aic@sue.cc.uregina.ca>
  9. NNTP-Posting-Host: sfogoros.hsc.unt.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. Tristan Psionic wrote:
  16. TP> 
  17. TP> In <31430CE8.469E@aol2.com>, Neil <neil@aol2.com> writes:
  18. TP> >I need help -- how can I open up a text file and just dump it
  19. TP> >all out (in plain text) to the screen?
  20. TP> Well, you can't do exactly that, but this is pretty close (and pretty raw):
  21. TP> 
  22. TP> #include <stdio.h>
  23. TP> 
  24. TP> void main (int argc,char argv[])
  25.  
  26. I have been reminded a number of times that main returns int and should be declared as 
  27. such. I agree in that script error level checking depends on main returning something to 
  28. indicate sucess or failure. I don't yet know if this is required by ANSI C. I have been 
  29. declaring main as returning void because I call exit() to end a program on both failure 
  30. and sucess. If I don't declare it void I get a compiler warning that the function should 
  31. return a value. Since main is not the function that sets the program return value, I 
  32. don't see why I should put in a return at the end of main just so I can declare it as 
  33. returning int. In this case, since the program ends at main you should declare it 
  34. returning int and return something.
  35.  
  36. Also, argv[] should be *argv[] or **argv
  37.  
  38. TP> {
  39. TP>   FILE *fp;
  40. TP>   char s[1000];
  41.  
  42. For my example, drop the array and add,
  43.  
  44. int c;
  45.  
  46. TP>   if (argc >= 1)
  47. TP>   {
  48. TP>     fp=fopen(argv[1],"r");
  49.  
  50. Test fp before using it. If it is equal to NULL you have a failure opening the file.
  51.  
  52. if(fp == NULL) return 10;
  53.  
  54. TP>     while (gets(s,1000,fp) != NULL)
  55. TP>     {
  56. TP>       printf("%s",s);
  57. TP>     }
  58.  
  59. Instead of taking a chance that the input string is longer than 1000 chars, you can read 
  60. and output a character at a time.
  61.  
  62. while((c = getc(fp)) != EOF)
  63.    putc(c,stdout);
  64.  
  65. TP>     fclose(fp);
  66. TP>   }
  67. TP>   else
  68. TP>   {
  69. TP>     printf("usage:  dumptext [filename]"
  70. TP>   }
  71.  
  72. return 0;
  73.  
  74. TP> }
  75. TP> 
  76. TP> That should work.  I'm not an expert at C or anything, but I do believe that
  77. TP> there aren't any hugely flawed errors in it.  Good luck.
  78. TP> -tristan
  79.  
  80. -- 
  81. Steve Fogoros, Academic Information Coordinator
  82. University of North Texas Health Science Center
  83. sfogoros@hsc.unt.edu
  84.